package com.tj.collection.common;


import com.fqgj.exception.common.ApplicationException;
import com.tj.collection.bean.RequestBasicInfo;
import com.tj.collection.db.domain.Admin;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 16/8/26
 * Time: 上午11:07
 */
public class RequestLocalInfo {
    private static final ThreadLocal<Admin> currentAdmin = new ThreadLocal<>();
    private static final ThreadLocal<RequestBasicInfo> currentRequestBasicVO = new ThreadLocal<>();

    public static void putCurrentAdmin(Admin user) {
        if (user == null) {
            throw new ApplicationException("Current user not exist");
        }
        currentAdmin.set(user);
    }

    public static Admin getCurrentAdmin() {
        Admin user = currentAdmin.get();
        if (user == null) {
            throw new ApplicationException("Current user not exist");
        }
        return user;
    }

    public static RequestBasicInfo getRequestBasicInfo() {
        RequestBasicInfo requestBasicInfo = currentRequestBasicVO.get();
        return requestBasicInfo;
    }

    /**
     * 当前请求的app参数信息
     *
     * @param requestBasicVO
     */
    public static void putCurrentRequestBasicVO(RequestBasicInfo requestBasicVO) {
        if (requestBasicVO == null) {
            return;
        }
        RequestLocalInfo.currentRequestBasicVO.set(requestBasicVO);
    }

    public static void reset() {
        currentAdmin.remove();
        currentRequestBasicVO.remove();
    }
}
